importPackage(java.io);
importPackage(java.util);
importPackage(java.nio.file);

load("scripts/test/test.js");
load("scripts/lib/panda/files.js");

test({
	name: function() {
		return "Test Hello";
	},
	testFileName: function() {
		var parameters, expected = "fileName";
		var req = {
			getParameter: function(name) {
				return parameters[name];
			}
		};

		verify("D:\\folder\\");
		verify("/home/pxx/");

		function verify(prefix) {
			parameters = { "Filedata" : prefix + expected };
			var actual = panda.fileName(req, "Filedata");
			Assert.assertEquals(expected, actual);
		}
	},
	testUploadWithMultipart: function() {
		var bytes = java.lang.String("Some Text").getBytes("UTF-8");
		var dest = "upload/1234-MutipartTest";
		var tmpFile = File.createTempFile("panda", null);
		var replace = StandardCopyOption.REPLACE_EXISTING;
		Files.copy(ByteArrayInputStream(bytes), tmpFile.toPath(), replace);
		
		var headers = {
			"Content-Type": java.lang.String("multipart/form-data")
		};
		var attributes = { "Filedata": tmpFile	};

		var req = {
			getHeader: function(name) {
				return headers[name];
			},
			getAttribute: function(name) {
				return attributes[name];
			}
		};

		var actualSize = panda.upload(req, "Filedata", dest);
		Assert.assertEquals(bytes.length, actualSize, 0);

		var file = new File(dest);
		Assert.assertTrue(file.exists());

		var o = ByteArrayOutputStream();
		Files.copy(file.toPath(), o);
		var actualBytes = o.toByteArray();
		Assert.assertTrue(Arrays.equals(bytes, actualBytes));

		file.deleteOnExit();
	},
	testUploadWithHtml5: function() {
		var bytes = java.lang.String("Some Text").getBytes("UTF-8");
		var dest = "upload/1234-Html5Test";

		var headers = {
			"Content-Type": java.lang.String("application/octet-stream")
		};

		var req = {
			getHeader: function(name) {
				return headers[name];
			},
			getInputStream: function() {
				return ByteArrayInputStream(bytes);
			}
		};

		var actualSize = panda.upload(req, undefined, dest);
		Assert.assertEquals(bytes.length, actualSize, 0);

		var file = new File(dest);
		Assert.assertTrue(file.exists());

		var o = ByteArrayOutputStream();
		Files.copy(file.toPath(), o);
		var actualBytes = o.toByteArray();
		Assert.assertTrue(Arrays.equals(bytes, actualBytes));

		file.deleteOnExit();
	},
	testDownload: function() {
		var bytes = java.lang.String("Some Text").getBytes("UTF-8");
		var name = "1234-DownloadTest";
		var src = "upload/" + name;

		var file = File(src);
		if (file.exists()) { file["delete"](); }

		Files.copy(ByteArrayInputStream(bytes), file.toPath());

		var req = {	getHeader: function() { return ""; } };
		var res = {
			outputStream: ByteArrayOutputStream(),
			setHeader: function() {}
		};

		panda.download(req, res, name, src);
		var actual = res.outputStream.toByteArray();
		Assert.assertTrue(Arrays.equals(bytes, actual));

		file.deleteOnExit();
	}
});
